<?php 
require_once 'Abstracts.php';
class GameModel extends Abstracts{
	
	protected $_table = 'pk_game';
	
	/**
	 * 保存游戏信息
	 * 自动将userInfo和publicPk自动序列化
	 * @param unknown_type $gameInfo
	 * @return boolean
	 */
	public function saveGame($gameInfo){
		if(!is_array($gameInfo) || !isset($gameInfo['id'])){
			return false;
		}
		if(isset($gameInfo['userInfo']) && is_array($gameInfo['userInfo']))
			$gameInfo['userInfo'] = serialize($gameInfo['userInfo']);
		if(isset($gameInfo['publicPk']) && is_array($gameInfo['publicPk']))
			$gameInfo['publicPk'] = serialize($gameInfo['publicPk']);
		if(isset($gameInfo['lookInfo']) && is_array($gameInfo['lookInfo']))
			$gameInfo['lookInfo'] = serialize($gameInfo['lookInfo']);
		if(isset($gameInfo['actUser']) && is_array($gameInfo['actUser']))
			$gameInfo['actUser']  = serialize($gameInfo['actUser']);					
		return $this->update($gameInfo);
	}
	/**
	 * 获取一条游戏信息 个字段都已经反序列化
	 * @param unknown_type $where
	 * @param unknown_type $order
	 * @param unknown_type $fields
	 * @return boolean|Ambigous <multitype:, mixed>
	 */
	public function getGameInfo($where = null,$order=null,$fields=null){
		$gameInfo = $this->getRowByWhere($where,$order,$fields);
		if(!$gameInfo)
			return false;
                $userinfo = array();
                
                if(!empty($gameInfo['userInfo'])){
                    $uinfo = unserialize($gameInfo['userInfo']);
                    foreach( $uinfo as $k=>$v ){
                        $v['pk'] = is_array($v['pk']) && !empty($v['pk']) ?$v['pk']:array();
                        $userinfo[$k] = $v;
                    }
                    ksort($userinfo);
                    $gameInfo['userInfo'] = $userinfo;
                }else{
                    ksort($userinfo);
                    $gameInfo['userInfo'] = $userinfo;
                }
		//$gameInfo['userInfo'] = empty($gameInfo['userInfo'])?array():unserialize($gameInfo['userInfo']);
		$gameInfo['publicPk'] = empty($gameInfo['publicPk'])?array():unserialize($gameInfo['publicPk']);
		$gameInfo['lookInfo'] = empty($gameInfo['lookInfo'])?array():unserialize($gameInfo['lookInfo']);
		$gameInfo['actUser']  = empty($gameInfo['actUser'])?array():unserialize($gameInfo['actUser']);
		return $gameInfo;
	}
}